package com.chujian.system.sys.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chujian.base.ICols;
import com.chujian.framework.core.base.BaseServiceImpl;
import com.chujian.framework.exception.MsgException;
import com.chujian.framework.util.EchoUtil;
import com.chujian.framework.util.TreeUs;
import com.chujian.sys.entity.SysDept;
import com.chujian.sys.entity.SysUser;
import com.chujian.system.sys.mapper.SysDeptMapper;
import com.chujian.system.sys.service.SysDeptService;
import com.chujian.system.sys.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 部门表 服务实现类
 *
 * @author chujian
 * @since 2021-07-28
 */
@Service
@Slf4j
public class SysDeptServiceImpl extends BaseServiceImpl<SysDeptMapper, SysDept> implements SysDeptService, ICols {

    @Autowired
    private SysUserService userService;
    @Autowired
    @Lazy
    private SysDeptService deptService;

    @Override
    public List<SysDept> getTreeDepts(String deptName) {
        List<SysDept> items = this.listAndFull(
                new LambdaQueryWrapper<SysDept>()
                        .like(StringUtils.isNotEmpty(deptName), SysDept::getDeptName, deptName)
                        .orderByAsc(SysDept::getDeptId)
        );

        if (CollUtil.isNotEmpty(items)) {
            return TreeUs.toEntityTree(items);
        }
        return null;
    }

    @Override
    public SysDept getPureDeptById(Long deptId) {
        QueryWrapper<SysDept> wrapper = new QueryWrapper<>();
        wrapper.eq(dept_id, deptId);
        SysDept dept = this.getOne(wrapper);
        return dept;
    }

    @Override
    public void full(List<SysDept> datas) {
        EchoUtil.echo(datas, "managerId", userService, (EchoUtil.IEchoCallback<SysDept, SysUser>) (item, value) -> item.setManager(value.getUserName()));
    }

    @Override
    public boolean beforeDel(List<Long> ids) {
        if (CollUtil.isNotEmpty(ids)) {
            for (Long id : ids) {
                List<SysUser> users = userService.getUsersByDeptId(id);
                if (users.size() > 0) {
                    throw new MsgException("已有用户绑定该部门，不可删除");

                } else {
                    return true;
                }
            }

        }
        return false;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public synchronized SysDept tryCreDept(String deptSid, String deptName) {
        SysDept dept = this.getBySourceId(deptSid);
        if (null == dept) {
            dept = new SysDept()
                    .setDeptName(deptName)
                    .setSourceId(deptSid);
            deptService.save(dept);
        }
        return dept;
    }

    @Override
    public SysDept getBySourceId(String sourceId) {
        return this.getOne(new QueryWrapper<SysDept>().eq(source_id, sourceId));
    }

    @Override
    public Map<Serializable, String> getEchoData(Collection<Serializable> ids) {
        return this.listByIds(ids)
                .stream()
                .collect(Collectors.toMap(SysDept::getDeptId, SysDept::getDeptName));
    }
}
